<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8"/>
    <title>数据管理</title>
    <link href="__STATIC__/component/pear/css/pear.css" rel="stylesheet"/>
</head>
<body class="pear-container">

<div class="layui-card">
    <div class="layui-card-body" style="padding:0;">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this"><a href="{:url('index')}">数据管理</a></li>
                <li><a href="{:url('restore')}">数据还原</a></li>
            </ul>
        </div>
        <table class="layui-hide" id="currentTable" lay-filter="currentTable"
               data-auth-backup="{:check_auth('backup')}"
               data-auth-optimize="{:check_auth('optimize')}"
               data-auth-repair="{:check_auth('repair')}"
               data-auth-structure="{:check_auth('structure')}"
               data-auth-del="{:check_auth('del')}"
        ></table>
    </div>
</div>

<script src="__STATIC__/component/layui/layui.js"></script>
<script src="__STATIC__/component/pear/pear.js"></script>
<script>
    layui.use(['aphpTable', 'aphp', 'table'], function () {
        let aphpTable = layui.aphpTable;
        let $ = layui.$;
        let aphp = layui.aphp;
        let table = layui.table;

        let init = {
            table_elem: '#currentTable',
            table_render_id: 'currentTable',
            delete_url: "{:url('del')}",
        };

        aphpTable.render({
            init: init,
            url: "{:url('index')}",
            toolbar: ['refresh_table',
                [{
                    text: '备份数据',
                    method: 'none',
                    auth: 'backup',
                    icon: 'layui-icon layui-icon-template-1',
                    class: 'layui-btn layui-btn-sm',
                    extend: 'data-event="backup"'
                }],
                [{
                    text: '优化表',
                    url: "{:url('optimize')}",
                    method: 'batch',
                    auth: 'optimize',
                    confirm: 'true',
                    icon: 'layui-icon layui-icon-leaf',
                    class: 'layui-btn layui-btn-normal layui-btn-sm',
                }],
                [{
                    text: '修复表',
                    url: "{:url('repair')}",
                    method: 'batch',
                    auth: 'repair',
                    confirm: true,
                    icon: 'layui-icon layui-icon-util',
                    class: 'layui-btn layui-btn-warm layui-btn-sm',
                }],
            ],
            search: false,
            cols: [
                [
                    {type: 'checkbox'},
                    {title: '表名', field: 'id', width: 150 },
                    {title: '备注', field: 'Comment'},
                    {title: '类型', field: 'Engine'},
                    {title: '编码', field: 'Collation'},
                    {title: '行数', field: 'Rows'},
                    {title: '大小', field: 'Data_length'},
                    {title: '碎片', field: 'Data_free'},
                    {title: '更新时间', field: 'Update_time'},
                    {
                        title: '操作',
                        align: 'center',
                        width: 210,
                        templet: aphpTable.formatter.tool,
                        operat: [
                            [{
                                title: '表结构',
                                text: '',
                                class: 'layui-btn layui-btn-sm',
                                icon: 'layui-icon layui-icon-slider',
                                url: "{:url('structure')}",
                                method: 'none',
                                auth: 'structure',
                                field_name: 'table',
                                extend: 'data-event="structure"',
                            }],
                            [{
                                title: '优化表',
                                text: '',
                                class: 'layui-btn layui-btn-normal layui-btn-sm',
                                icon: 'layui-icon layui-icon-leaf',
                                url: "{:url('optimize')}",
                                method: 'request',
                                auth: 'optimize',
                                field_name: 'ids',
                                confirm: true,
                            }],
                            [{
                                title: '修复表',
                                text: '',
                                class: 'layui-btn layui-btn-warm layui-btn-sm',
                                icon: 'layui-icon layui-icon-util',
                                url: "{:url('repair')}",
                                method: 'request',
                                auth: 'repair',
                                field_name: 'ids',
                                confirm: true,
                            }],
                            [{
                                title: '删除表',
                                text: '',
                                class: 'layui-btn layui-btn-danger layui-btn-sm',
                                icon: 'layui-icon layui-icon layui-icon-delete',
                                url: "{:url('del')}",
                                method: 'request',
                                auth: 'del',
                                field_name: 'table',
                                confirm: true,
                            }],
                        ]
                    }
                ]
            ],
            page: {}
        });
        aphpTable.bindEvent();

        // 备份事件
        $(document).on('click', "[data-event='backup']", function () {
            var data = table.checkStatus(init.table_render_id).data;
            var ids = '';
            var msg = '确认要备份全部数据吗？';
            if (data.length > 0) {
                for (let i = 0; i < data.length; i++) {
                    ids += data[i].Name + ",";
                }
                ids = ids.slice(0, -1);
                msg = '确认要备份以下表吗？<br/>' + ids;
            }
            layer.confirm(msg, {btn: ['确定', '取消']}, function (index) {
                layer.close(index);
                var loadIndex = layer.msg('数据备份中，请耐心等待……', {icon: 16, shade: 0.01, time: 60000});
                $.post("{:url('database/backup')}", {ids: ids}, function (ret) {
                    layer.close(loadIndex);
                    if (ret.status === 1) {
                        layer.msg(ret.msg, {icon: 1, time: 1000}, function () {
                            window.location.href = ret.url;
                        });
                    } else {
                        layer.msg(ret.msg, {icon: 2, time: 2000});
                    }
                }, 'json');
            });
            return false;
        });

        // 查看表结构
        $(document).on('click', "[data-event='structure']", function () {
            var url = $(this).data('href');
            $.get(url, function (ret) {
                if (ret.status === 1) {
                    layer.open({
                        type: 1,
                        title: ret.table + ' 表结构： ',
                        shade: 0.1,
                        content: '<div style="padding: 15px;"><pre id="copy">' + ret.data + '</pre><hr/><div style="text-align: right;"><span class="layui-btn layui-btn-sm copy" data-copy-by="#copy">复制表结构</span></div></div>',
                    });
                } else {
                    layer.msg(ret.msg, {icon: 2, time: 2000});
                }
            }, 'json');
            return false;
        });

    });
</script>
</body>
</html>